package com.im.app.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.im.app.mapper.OfflineReplyMapper;
import com.im.app.model.OfflineReply;
import com.im.app.model.User;
import com.im.app.service.IOfflineReplyService;
import com.im.app.utils.text.Convert;
import org.jim.core.cache.redis.JedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-12
 */
@Service
public class OfflineReplyServiceImpl implements IOfflineReplyService
{
    @Resource
    private OfflineReplyMapper offlineReplyMapper;


    private void cacheOfflineReplys(List<OfflineReply> list,String userId){
        if(list == null || userId == null) return;
        String key = "offline_"+userId;
        try {
            JedisTemplate.me().set(key,list,60*60*24*2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<OfflineReply> getRedisOfflineReplys(String key){
        List<OfflineReply> offlineReplyList = new ArrayList<>();
        try {
            List<JSONObject> list = JedisTemplate.me().get(key, List.class);
            if(null!=list && list.size()>0){
                for (JSONObject obj: list) {
                    OfflineReply offlineReply = obj.toJavaObject(OfflineReply.class);
                    offlineReplyList.add(offlineReply);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return offlineReplyList;
    }

    private void delCacheOfflineReplys(String userId){
        String key = "offline_"+userId;
        try {
            JedisTemplate.me().delKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public OfflineReply selectOfflineReplyById(Long id)
    {
        return offlineReplyMapper.selectOfflineReplyById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param offlineReply 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<OfflineReply> selectOfflineReplyList(OfflineReply offlineReply)
    {
        if(offlineReply==null) return null;
        String key = "offline_"+offlineReply.getUserId();
        List<OfflineReply> offlineReplys = getRedisOfflineReplys(key);
            if(offlineReplys == null){
                offlineReplys = offlineReplyMapper.selectOfflineReplyList(offlineReply);
                if(offlineReplys != null){
                    cacheOfflineReplys(offlineReplys,offlineReply.getUserId());
                }
            }
        return offlineReplys;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param offlineReply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOfflineReply(OfflineReply offlineReply)
    {
        int result = offlineReplyMapper.insertOfflineReply(offlineReply);
        if(result>0){
            OfflineReply off = new OfflineReply();
            off.setUserId(offlineReply.getUserId());
            List<OfflineReply> offlineReplyList = offlineReplyMapper.selectOfflineReplyList(off);
            cacheOfflineReplys(offlineReplyList,offlineReply.getUserId());
        }
        return result;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param offlineReply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOfflineReply(OfflineReply offlineReply)
    {
        int result = offlineReplyMapper.updateOfflineReply(offlineReply);
        if(result>0){
            OfflineReply off = new OfflineReply();
            off.setUserId(offlineReply.getUserId());
            List<OfflineReply> offlineReplyList = offlineReplyMapper.selectOfflineReplyList(off);
            cacheOfflineReplys(offlineReplyList,offlineReply.getUserId());
        }
        return result;
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteOfflineReplyByIds(String ids)
    {
        return offlineReplyMapper.deleteOfflineReplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteOfflineReplyById(User user , Long id)
    {
        int result = offlineReplyMapper.deleteOfflineReplyById(id);
        if(result>0){
            OfflineReply off = new OfflineReply();
            off.setUserId(user.getUserId());
            List<OfflineReply> offlineReplyList = offlineReplyMapper.selectOfflineReplyList(off);
            cacheOfflineReplys(offlineReplyList,user.getUserId());
        }
        return result;
    }
}
